//
//  MasterCookBookViewController.m
//  Taste
//
//  Created by 徐昊 on 16/9/19.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "MasterCookBookViewController.h"
#import "CookBookMateralTableViewCell.h"
#import "CookBookStepTableViewCell.h"
#import "CookBookMoreTableViewCell.h"
#import "ZWYNetTool.h"
#import "HXMacro.h"
#import "NSString+Vali.h"
#import "UIView+Frame.h"
#import "MasterCookBookModel.h"
#import "MasterCookBookRes.h"
#import "MasterCookBookMaterals.h"
#import "MasterCookBookMl.h"
#import "MasterCookBookSteps.h"
#import "MasterCookBookTips.h"
#import "UIImageView+WebCache.h"
#import "SelfSizeTool.h"
#import "VideoWebViewController.h"

#define kCookBookMateralTableViewCellName @"CookBookMateralTableViewCellNameReuse"
#define kCookBookStepTableViewCellName @"CookBookStepTableViewCellNameReuse"
#define kCookBookMoreTableViewCellName @"CookBookMoreTableViewCellNameReuse"
@interface MasterCookBookViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIImageView *masterCookBookPic;
@property (weak, nonatomic) IBOutlet UILabel *dishTitle;
@property (weak, nonatomic) IBOutlet UILabel *category;
@property (weak, nonatomic) IBOutlet UILabel *author;
@property (weak, nonatomic) IBOutlet UILabel *preTime;
@property (weak, nonatomic) IBOutlet UILabel *cookTime;
@property (weak, nonatomic) IBOutlet UILabel *personNum;
@property (weak, nonatomic) IBOutlet UIImageView *video;
@property (nonatomic, strong) MasterCookBookModel *model;

@end

@implementation MasterCookBookViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
    NSString *bodyString = [NSString stringWithFormat:@"id=%@", _numString];
    NSString *bodyLength = [NSString stringWithFormat:@"%ld", [bodyString length]];
    NSDictionary *dic = @{@"version" : @"2.50",
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : bodyLength};
    [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=cookbook&a=getinfo" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [MasterCookBookModel modelWithDic:result];
        [_masterCookBookPic sd_setImageWithURL:[NSURL URLWithString:[NSString imageWebWithStr:_model.res.pic]] placeholderImage:[UIImage imageNamed:@"IMG_Universal_Placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        _dishTitle.text = _model.res.title;
        _category.text = _model.res.masterCookBookRes_typename;
        _author.text = _model.res.authorname;
        _preTime.text = _model.res.pretime;
        _cookTime.text = _model.res.maketime;
        _personNum.text = _model.res.people;
        _headerView.h_height = self.view.h_width * 4 / 3 + 140;
        if ([_model.res.video isEqualToString:@""]) {
            _video.alpha = 0;
        } else {
            _video.alpha = 1;
            _masterCookBookPic.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(push)];
            [_masterCookBookPic addGestureRecognizer:tap];
        }
        _tableView.tableHeaderView = _headerView;
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}
- (void)push {
    VideoWebViewController *vc = [[VideoWebViewController alloc] init];
    vc.urlStr = _model.res.video;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"CookBookMateralTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCookBookMateralTableViewCellName];
    [_tableView registerNib:[UINib nibWithNibName:@"CookBookStepTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCookBookStepTableViewCellName];
    [_tableView registerNib:[UINib nibWithNibName:@"CookBookMoreTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCookBookMoreTableViewCellName];
}
- (IBAction)pop:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section < _model.res.materals.count) {
        return _model.res.materals[section].ml.count;
    } else if (section == (_model.res.materals.count) && _model.res.steps.count) {
        return _model.res.steps.count;
    } else if (section == (_model.res.materals.count + 1) && _model.res.tips.count) {
        return _model.res.tips.count;
    } else {
        return 1;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger num = 0;
    if (_model.res.tips.count) {
        num += 1;
    }
    if (_model.res.steps.count) {
        num += 1;
    }
    return _model.res.materals.count + num + 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section < _model.res.materals.count) {
        return 20;
    } else if (indexPath.section == (_model.res.materals.count) && _model.res.steps.count) {
        return [SelfSizeTool selfSizeHeightWith:_model.res.steps[indexPath.row].content width:self.view.h_width - 50  font:[UIFont systemFontOfSize:17]] + 10;
    } else if (indexPath.section == (_model.res.materals.count + 1) && _model.res.tips.count) {
        return [SelfSizeTool selfSizeHeightWith:_model.res.tips[indexPath.row].content width:self.view.h_width - 50  font:[UIFont systemFontOfSize:17]] + 10;
    } else {
        return (SCREEN_WIDTH / 3 + 40) * 2;
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section < _model.res.materals.count) {
        return _model.res.materals[section].catalog;
    } else if (section == (_model.res.materals.count) && _model.res.steps.count) {
        return @"制作步骤";
    } else if (section == (_model.res.materals.count + 1) && _model.res.tips.count) {
        return @"小贴士";
    } else {
        return @"相关菜谱";
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section < _model.res.materals.count) {
        CookBookMateralTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCookBookMateralTableViewCellName];
        cell.ml = _model.res.materals[indexPath.section].ml[indexPath.row];
        return cell;
    } else if (indexPath.section == (_model.res.materals.count) && _model.res.steps.count) {
        CookBookStepTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCookBookStepTableViewCellName];
        cell.steps = _model.res.steps[indexPath.row];
        return cell;
    } else if (indexPath.section == (_model.res.materals.count + 1) && _model.res.tips.count) {
        CookBookStepTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCookBookStepTableViewCellName];
        cell.tips = _model.res.tips[indexPath.row];
        return cell;
    } else {
        CookBookMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCookBookMoreTableViewCellName];
        cell.array = _model.res.more;
        return cell;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
